document.observe('dom:loaded', function() {
    // loadboxEvents();
});

/*
Add by Quang 
10/02/2010
Load cac event cho trang Dang ky, Dang nhap moi
*/
function loadboxEvents() {
    var url = document.location.pathname.toLowerCase();
    if (url.indexOf('dangkypartner.aspx') != -1) {
        loadDDLs(); //load DDL cho trang Dang ky partner
    }
    else if (url.indexOf('bosungthongtin.aspx') != -1) {
        loadDDLs();
    }
    else if (url.indexOf('dangky.aspx') != -1) { }
    //edit 03/04/2010
    else if (url.indexOf('dangnhap.aspx') != -1 || url.indexOf('index.aspx') != -1) {
        loadCheckboxRemember($('chk_remember'));
    }
    //cac textbox
    loadTextboxEvent($(document.body));

    //cac checkbox
    //edit 03/04/2010
    if (url.indexOf('dangnhap.aspx') == -1 && url.indexOf('index.aspx') == -1) {
        loadCheckBox(); //checkbox kieu fai chon 1 trong 2
    }

};

/*
Add by Quang 
10/02/2010
Load cac event cho checkbox
*/
function loadCheckboxRemember(checkID) {
    if (checkID) {
        var arrLabel;
        try {
            arrLabel = checkID.up().down('label');
        } catch (ee) { arrLabel = $('lb_remember') }
        arrLabel.observe('click', function(e) {
            var ele = Event.element(e).up('div');
            if (ele.hasClassName('checked')) ele.removeClassName('checked');
            else ele.addClassName('checked');
        });
    }
};

/*DROP DOWN LIST*/
/*
Add by Quang 
10/02/2010
Thay the cac element select mac dinh bang Select kieu moi
*/
function loadDDLs() {
    var ddlDate = new ddlDD();
    ddlDate.initializeCtrl('ddlDate', onLoadDDL, onClickDDL);
    var ddlMonth = new ddlDD();
    ddlMonth.initializeCtrl('ddlMonth', onLoadDDL, onClickDDL);
    var ddlYear = new ddlDD();
    ddlYear.initializeCtrl('ddlYear', onLoadDDL, onClickDDL);
};

/*
Add by Quang 
10/02/2010
Ham duoc goi khi tao Select kieu moi
*/
/*edit here 08/03/2011*/
function onLoadDDL(obj) {
    if (obj.scroller == null) {
        var id = obj.targetID;
        //if (!$(id).hasClassName("ddlNew")) {
            obj.scroller = new Scrolling.Scroller($(id + '_ddlScroller'), 400, 165);
            obj.scrollbar = new Scrolling.Scrollbar($(id + '_ddlScrollbar'), obj.scroller, new Scrolling.ScrollTween());
        //}
        //        if (id != "ddlCityNew") {
        //             obj.scroller = new Scrolling.Scroller($(id + '_ddlScroller'), 400, 165);
        //             obj.scrollbar = new Scrolling.Scrollbar($(id + '_ddlScrollbar'), obj.scroller, new Scrolling.ScrollTween());
        //        }
    }
};
/*end*/

/*
Add by Quang 
10/02/2010
Event click tren Dropdownlist
*/
function onClickDDL(id, value) {
    var selectedValue = value;
    //hoac lay gia tri tu dropdown cung dc
    //alert($(id).value)
};

/*
Add by Quang 
10/02/2010
Object Dropdownlist
*/
var ddlDD = Class.create({
    initialize: function() {
        this.targetID = null;
        this.DDL = null;
        this.selectedValue = '';
        this.onClickHandle = null;
        this.onLoadHandle = null;
        this.scroller = null;
        this.scrollbar = null;
        this.Height = null;
    },
    /*
    Add by Quang 
    10/02/2010
    Ham khoi tao
    */
    initializeCtrl: function(targetID, onLoadHandle, onClickHandle, isSmall) {
        var select = $(targetID);
        if (select) {
            this.targetID = targetID;
            this.onClickHandle = onClickHandle;
            this.onLoadHandle = onLoadHandle;
            this.loadHtml(select, isSmall);
        }
    },
    /*
    Add by Quang 
    10/02/2010
    Ham thay the noi dung html
    */
    /*edit here 11/01/2011*/
    loadHtml: function(objSelect, isSmall) {

        var id = this.targetID;
        var objSelected = objSelect.options[objSelect.selectedIndex];
        var widthobject = (objSelect.offsetWidth + 13);
        this.selectedValue = objSelected.value;

        if ($(id).hasClassName("ddlNew")) {
            /*edit here 22/04/2011 Dll Quan Moi*/
            var widthDDL = $(id).offsetWidth;
            var html = '<div class="div_bgListCity" style="width:' + widthDDL + 'px">' +
                         '<i class="div_LeftDDLCity"></i>' +
                          '<div class="div_BodyDDLCity" style="width:' + (widthDDL - 32) + 'px">' +
                            '<h1 class="styleText11 color6 strCity" unselectable="on" id="str_' + id + '">' + objSelected.text + '</h1>' +
                             '<i class="div_btDDLCity"></i>' +
                          '</div>' +
                         '<i class="div_RightDDLCity"></i>' +
                        '</div>' +
                        '<div class="select_list" id="select_' + id + '" style="width:' + widthDDL + 'px">' +
                            '<div class="div_TopDDLNew"><i class="itlDDLNew"></i><i class="ctDDL" style="width:' + (widthDDL - 40) + 'px"></i><i class="itrDDLNew"></i></div>' +
                                '<div class="dInnerDDL">' +
                                    '<div class="dinnerIE6">' +
                                    '<ul id="' + id + '_ddlScroller" style="position:relative;#zoom:1">';
            for (var i = 0, len = objSelect.options.length; i < len; i++) {
                var option = objSelect.options[i];
                html += '<li id="' + id + '_' + option.value + '"><a href="javascript:void(0)">' + option.text + '</a></li>';
            }
            html += '</ul>' +
                    '</div>' +
                '<div id="' + id + '_ddlScrollbar" class="select_scrollbar" style="display:block">' +
			        '<div class="Scrollbar-Up"></div>' +
			        '<div class="Scrollbar-Down"></div>' +
			        '<div class="Scrollbar-Track">' +
				        '<div class="Scrollbar-Handle" style="top: 0; display: block;"></div>' +
				    '</div>' +
				  '</div>' +
                                '</div>' +
                          '<div class="div_TopDDLNew"><i class="iblDDLNew"></i><i class="ctDDL" style="width:' + (widthDDL - 40) + 'px"></i><i class="brDDL"></i></div>' +
                        '</div>';
            this.DDL = new Element('div', { 'id': id + '_ddl', 'class': 'dTopCityNew', 'style': 'width:' + widthDDL + 'px' }).update(html);
            /*end*/
            //            var html = '<div class="div_bgListCity">' +
            //                         '<i class="div_LeftDDLCity"></i>' +
            //                          '<div class="div_BodyDDLCity">' +
            //                            '<h1 class="styleText11 color6 strCity" unselectable="on" id="str_' + id + '">' + objSelected.text + '</h1>' +
            //                             '<i class="div_btDDLCity"></i>' +
            //                          '</div>' +
            //                         '<i class="div_RightDDLCity"></i>' +
            //                        '</div>' +
            //                        '<div class="select_list" id="select_' + id + '">' +
            //            //                            '<i class="tl_DDL"></i>' +
            //                            '<div class="div_TopDDLNew"><i class="itlDDLNew"></i><i class="ctDDL"></i><i class="trDDL"></i></div>' +
            //            //                            '<i class="tr_DDL"></i>' +
            //            //                             '<div class="dAbsoluteDDL">' +
            //                                '<div class="dInnerDDL">' +
            //                                    '<ul>';
            //            for (var i = 0, len = objSelect.options.length; i < len; i++) {
            //                var option = objSelect.options[i];
            //                html += '<li id="' + id + '_' + option.value + '"><a href="javascript:void(0)">' + option.text + '</a></li>';
            //            }
            //            html += '</ul>' +
            //                                '</div>' +
            //            //                            '</div>' +
            //            //                            '<i class="bl_DDL"></i>' +
            //                             '<div class="div_TopDDLNew"><i class="iblDDLNew"></i><i class="ctDDL"></i><i class="brDDL"></i></div>' +
            //            //                            '<i class="br_DDL"></i>' +
            //                        '</div>';
            //            this.DDL = new Element('div', { 'id': id + '_ddl', 'class': 'dTopCityNew' }).update(html); 
        }
        else {
            var html = '<i class="iLDLL"></i>' +
    '<div class="iBDLL" style="width:' + (widthobject - 28) + 'px"><strong unselectable="on" class="no_select" id="str_' + id + '">' + objSelected.text + '</strong></div>' +
    '<i class="iRDLL"></i>' +
	        '<div class="select_list" id="select_' + id + '">' +
		        '<ul id="' + id + '_ddlScroller" class="select_options" style="#zoom:1">';
            for (var i = 0, len = objSelect.options.length; i < len; i++) {
                var option = objSelect.options[i];
                html += '<li id="' + id + '_' + option.value + '">' + option.text + '</li>';
            }
            html += '</ul>' +
                '<span class="' + (id == 'ddlSL_0' ? 'spLineddl_SL' : 'spLineddl') + '"></span>' +
		        '<div id="' + id + '_ddlScrollbar" class="select_scrollbar" style="display:block">' +
			        '<div class="Scrollbar-Up"></div>' +
			        '<div class="Scrollbar-Down"></div>' +
			        '<div class="Scrollbar-Track">' +
				        '<div class="Scrollbar-Handle" style="top: 0; display: block;"></div></div></div></div>'; //Quang 23/02
            //Quang 23/02

            this.DDL = new Element('div', { 'id': id + '_ddl', 'class': 'select' + (!isSmall ? '' : ' select_small'), 'style': 'width:' + widthobject + 'px;' }).update(html);
        }
        objSelect.insert({ 'before': this.DDL }).hide();
        if (this.onLoadHandle && typeof (this.onLoadHandle) == 'function') {
            this.onLoadHandle(this);
        }
        this.Height = this.DDL.down('.select_list').offsetHeight;
        this.DDL.down('.select_list').hide();
        this.loadEvents();
        try {
            showDropDownForMobile();
        } catch (ex) { } 
    },
    loadEvents: function() {
        this.DDL.observe('click', this.DDLClick.bindAsEventListener(this));
        document.observe('click', this.docClick);
    },
    DDLClick: function(e) {
        var ele = Event.element(e);

        while (ele.id == '') ele = ele.up();
        if (ele.id == this.targetID + '_SV' || ele.id == this.targetID + '_ddl') {
            var ddlList = this.DDL.down('.select_list');
            if (ddlList.visible()) {
                //                if (this.targetID == "ddlCityNew") {
                //                    openCollapse('select_ddlCityNew', this.Height);
                //                }
                if ($(this.targetID).hasClassName("ddlNew")) {
                    openCollapse('select_' + this.targetID, this.Height);
                }
                else { ddlList.hide(); }
            }
            else {
                this.docClick(e);
                //                if (this.targetID == "ddlCityNew") {
                //                    openCollapse('select_ddlCityNew', this.Height);
                //                }
                if ($(this.targetID).hasClassName("ddlNew")) {
                    openCollapse('select_' + this.targetID, this.Height);
                }
                else { ddlList.show(); }
            }
        }
        else if (ele.tagName == 'LI') {
            var newValue = ele.id.replace(this.targetID + '_', '');
            if (this.selectedValue != newValue) {
                this.selectedValue = newValue;
                $(this.targetID).value = this.selectedValue;
                if (this.onClickHandle && typeof (this.onClickHandle) == 'function') {
                    this.onClickHandle(this.targetID, this.selectedValue);
                }
            }
            //            if (this.targetID == "ddlCityNew") {
            //                openCollapse('select_ddlCityNew', this.Height);
            //            }
            if ($(this.targetID).hasClassName("ddlNew")) {
                openCollapse('select_' + this.targetID, this.Height);
            }
            else { this.DDL.down('.select_list').hide(); }
        }
        Event.stop(e);
    },
    /*
    Add by Quang 
    10/02/2010
    Event click tren 1 element khac ddl, hide ddl di
    */
    docClick: function(e) {
        //        if ($("ddlCityNew") && $("select_ddlCityNew").style.display == "block") {
        //            $('select_ddlCityNew').style.height = $('select_ddlCityNew').offsetHeight + 'px';
        //            scrollLock("select_ddlCityNew", $('select_ddlCityNew').offsetHeight);
        //        }
        //        else {
        $(document.body).select('.select_list').invoke('hide');
        // }
    }
});
/*end*/
/*END DROP DOWN LIST*/


